package tt.dz.appserver.service.business.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.avro.AvroRemoteException;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.entity.vo.car.CarControlInfoVo;
import tt.dz.appserver.entity.vo.car.CarInfoVo;
import tt.dz.appserver.entity.vo.car.CarJourneyVo;
import tt.dz.appserver.entity.vo.car.CarLocationVo;
import tt.dz.appserver.service.avro.client.car.DzVehicleInternetRPCProtocol;
import tt.dz.appserver.service.business.CarService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.enums.CarOpResult;
import tt.dz.appserver.util.enums.Response;
import tt.dz.appserver.util.json.JsonUtils;

/**
 * 汽车业务实现类
 * 
 * @author Administrator
 *
 */
public class CarServiceImpl implements CarService {

	/**日志*/
	private static final Logger LOG = Logger.getLogger("[车联网汽车 操作业务接口]:");

	/**rpc 对象注入 */
	@Resource(name = "carMessageProtocolRequestor")
	private DzVehicleInternetRPCProtocol carRpcService;

	@Override
	public ResultMappingEntity carList() throws AvroRemoteException {

		// 查询汽车列表
		String carListJsonString = carRpcService.queryVehicleList().toString();
		JSONObject carListJson = JSONObject.parseObject(carListJsonString);
		LOG.info("汽车列表返回结果:" + carListJsonString);

		String opCode = carListJson.getString("retCode");
		if (CarOpResult.SUCCESS.getCode().equals(opCode)) {

			JSONArray carList = carListJson.getJSONArray("bizData");
			List<CarInfoVo> carInfoList = new ArrayList<CarInfoVo>();
			for (int i = 0; i < carList.size(); i++) {
				JSONObject carInfoObject = carList.getJSONObject(i);
				CarInfoVo carInfo = JsonUtils.JsonToObjectOfClass(carInfoObject, CarInfoVo.class);
				carInfoList.add(carInfo);
			}

			return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), carInfoList);
		} else {
			return Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg());
		}
	}

	@Override
	public ResultMappingEntity controlCar(String vin, String controlComCode, String controlAction)
			throws AvroRemoteException {

		//拼装传递参数
		Map<String, String> params = new HashMap<String, String>();
		params.put("vin", vin);// 车辆Vin号码
		params.put("command_no", controlComCode);// 控制命令
		params.put("control_content", controlAction);// 控制内容/动作

		String passParamsStr = JSONObject.toJSONString(params);
		String carControlControlJsonString = carRpcService.sendRemoteCmd(passParamsStr).toString();
		JSONObject carControlJson = JSONObject.parseObject(carControlControlJsonString);
		LOG.info("发送命令控制【 "+vin+" 】汽车,控制命令为【 "+controlComCode+" 】,控制动作为【 "+controlAction+" 】返回结果:" + carControlControlJsonString);

		String opCode = carControlJson.getString("retCode");
		if (CarOpResult.SUCCESS.getCode().equals(opCode)) {

			return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg());
		} else {
			return Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg());
		}
	}

	@Override
	public ResultMappingEntity controlCommand(String vin) throws AvroRemoteException {

		String carControlCommandJsonString = carRpcService.queryVehicleSupportedCmd(vin).toString();
		JSONObject carCommandJson = JSONObject.parseObject(carControlCommandJsonString);
		LOG.info("查询【 "+vin+" 】汽车支持的命令返回结果:" + carControlCommandJsonString);

		String opCode = carCommandJson.getString("retCode");
		if (CarOpResult.SUCCESS.getCode().equals(opCode)) {

			// 获取业务数据
			JSONObject carControlInfoObject = carCommandJson.getJSONObject("bizData");

			// 封装字段
			CarControlInfoVo carControlInfo = JsonUtils.JsonToObjectOfClass(carControlInfoObject,
					CarControlInfoVo.class);

			return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), carControlInfo);
		} else {
			return Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg());
		}
	}

	@Override
	public ResultMappingEntity getCarLocation(String vin) throws AvroRemoteException {

		String carControlCommandJsonString = carRpcService.queryVehicleLocation(vin).toString();
		JSONObject carCommandJson = JSONObject.parseObject(carControlCommandJsonString);
		LOG.info("查询【 "+vin+" 】汽车支持的位置信息返回结果:" + carControlCommandJsonString);

		String opCode = carCommandJson.getString("retCode");
		if (CarOpResult.SUCCESS.getCode().equals(opCode)) {

			// 获取业务数据
			JSONObject carLocationInfoObject = carCommandJson.getJSONObject("bizData");
			if(carLocationInfoObject==null){

				return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg());
			}
			// 封装字段
			CarLocationVo carLocation = JsonUtils.JsonToObjectOfClass(carLocationInfoObject,
					CarLocationVo.class);

			return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), carLocation);
		} else {
			return Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg());
		}
	}

	@Override
	public ResultMappingEntity journey(String vin) throws AvroRemoteException {
		
		String carControlCommandJsonString = carRpcService.getVehicleRunningData(vin).toString();
		JSONObject carCommandJson = JSONObject.parseObject(carControlCommandJsonString);
		LOG.info("查询【 "+vin+" 】辆车的行驶数据返回结果:" + carControlCommandJsonString);

		String opCode = carCommandJson.getString("retCode");
		if (CarOpResult.SUCCESS.getCode().equals(opCode)) {

			// 获取业务数据
			JSONObject jsonObject = carCommandJson.getJSONObject("bizData");
			// 封装字段
			CarJourneyVo carJourneyVo = JsonUtils.JsonToObjectOfClass(jsonObject, CarJourneyVo.class);
			return Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), carJourneyVo);
		} else {
			return Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg());
		}
	}
}
